#include "global.h"
#include <bits/stdc++.h>
#include <unistd.h>

int getPath();
int readFile();
int readControlDict(char *fileName);
int readAtomMass(char *fileName);
int readCompound(char *fileName);
int ele2Num(const string name);

using namespace std;

int getPath()
{
    char tempPath[255];
    size_t size=255;
    if(!getcwd(tempPath,size)){
        cout<<"Get path fialed!"<<endl;
        return -1;
    }
    cout<<"Current path:"<<tempPath<<endl;
    strcpy(currentPath,tempPath);
    return 0;
}

int readFile()
{
    readAtomMass((char *)"/atomMass");
    //readControlDict((char *)"/controlDict");
    readCompound((char *)"/compoundData");
    return 0;
}

int readControlDict(char *fileName)
{
    char *filePath=currentPath;
    strcat(filePath,fileName);
    cout<<"Reading controlDict from "<<filePath<<endl;
    freopen(filePath,"r",stdin);
    string tempstr;
    int tempInt;
    double tempDouble;
    while(cin>>tempstr){
        if(tempstr=="test1")cout<<"1111111testtesttesttest"<<endl; 
        cin>>test;
        cout<<tempstr<<"="<<test<<endl;
    }
    fclose(stdin);
    cout<<"Done reading controlDict."<<endl;
    return 0;
}

int readAtomMass(char *fileName)
{
    //unit:g/mol
    char filePath[256];
    strcpy(filePath,currentPath);
    strcat(filePath,fileName);
    cout<<"Reading atomMass from "<<filePath<<endl;
    //freopen(filePath,"r",stdin);
    ifstream fin(filePath);
    streambuf *oldcin;
    oldcin = cin.rdbuf(fin.rdbuf());
    string tempstr;
    int tempInt;
    double tempDouble;
    int i=0;
    while(cin>>tempstr){
        ele[i].name=tempstr;
        cin>>ele[i].mass;
        ele[i].mass/=1000;
        //kg/mol
        ele[i].num=i+1;
        //cout<<ele[i].name<<"\t"<<ele[i].mass<<endl;
        i++;
    }
    eleNum=i;
    cout<<"Done reading AtomMass."<<endl;
    //fclose(stdin);
    cin.rdbuf(oldcin);
    fin.close();
    return 0;
}

int readCompound(char *fileName){
    char filePath[256];
    strcpy(filePath,currentPath);
    strcat(filePath,fileName);
    cout<<"Reading compoundData from "<<filePath<<endl;
    ifstream fin(filePath);
    streambuf *oldcin;
    oldcin = cin.rdbuf(fin.rdbuf());
    string tempstr;
    int tempInt;
    double tempDouble;
    int i=0;
    while(cin>>tempstr){
        compound[i].name=tempstr;
        cin>>compound[i].phase;
        cin>>tempInt;
        //Number of elements
        for(int j=0;j<tempInt;j++){
            string tempstr2;
            cin>>tempstr2;
            cin>>compound[i].atoNum[ele2Num(tempstr2)-1];
            compound[i].mass+=ele[ele2Num(tempstr2)-1].mass*compound[i].atoNum[ele2Num(tempstr2)-1];
        }
        cin>>compound[i].itnNum;
        //Number of T internals
        for(int j=0;j<compound[i].itnNum;j++){
            cin>>compound[i].tLB[j];
            for(int k=0;k<7;k++){
                cin>>compound[i].a[j][k];
            }
            cin>>compound[i].b[j][0];cin>>compound[i].b[j][1];
        }
        cout<<"Compound:"<<"\t"<<compound[i].name<<"\tMolecular weight:\t"<<compound[i].mass<<"\tkg/mol"<<endl;
        i++;
    }
    compoundNum=i;
    cout<<"Done reading compoundData."<<endl;
    cin.rdbuf(oldcin);
    fin.close();
    return 0;
}

int readFuel(char *fileName)
{
    //trans filePath
    char filePath[256];
    strcpy(filePath,currentPath);
    strcat(filePath,fileName);
    cout<<"Reading fuelData from "<<filePath<<endl;
    //redirect
    ifstream fin(filePath);
    streambuf *oldcin;
    oldcin = cin.rdbuf(fin.rdbuf());
    //read fuelData
    string tempstr;
    int tempint;
    int i=0;
    while(cin>>tempstr){
        fuel[i].name=tempstr;
        cin>>tempint;
        for(int j=0;j<tempint;j++){
            string tempstr2;
            cin>>tempstr2;
            cin>>fuel[i].atoNum[ele2Num(tempstr2)-1];
            fuel[i].mass+=ele[ele2Num(tempstr2)-1].mass*fuel[i].atoNum[ele2Num(tempstr2)-1];
            //molecular weight
        }
        double tempMas;
        cin>>fuel[i].h;
        cin>>tempMas;
        fuel[i].mol=tempMas/fuel[i].mass;
        cout<<"Fuel:\t"<<fuel[i].name<<"\tMolecular weight:\t"<<fuel[i].mass<<" kg/mol"<<endl;
        i++;
    }
    fuelNum=i;
    //redirect
    cout<<"Done reading fuelData."<<endl;
    cin.rdbuf(oldcin);
    fin.close();
    return 0;
}

int readOxidizer(char *fileName)
{
    //trans filePath
    char filePath[256];
    strcpy(filePath,currentPath);
    strcat(filePath,fileName);
    cout<<"Reading oxidizerData from "<<filePath<<endl;
    //redirect
    ifstream fin(filePath);
    streambuf *oldcin;
    oldcin = cin.rdbuf(fin.rdbuf());
    //read oxidizerData
    string tempstr;
    int tempint;
    int i=0;
    while(cin>>tempstr){
        oxidizer[i].name=tempstr;
        cin>>tempint;
        for(int j=0;j<tempint;j++){
            string tempstr2;
            cin>>tempstr2;
            cin>>oxidizer[i].atoNum[ele2Num(tempstr2)-1];
            oxidizer[i].mass+=ele[ele2Num(tempstr2)-1].mass*oxidizer[i].atoNum[ele2Num(tempstr2)-1];
            //molecular weight
        }
        double tempMas;
        cin>>oxidizer[i].h;
        cin>>tempMas;
        oxidizer[i].mol=tempMas/oxidizer[i].mass;
        cout<<"Oxidizer:\t"<<oxidizer[i].name<<"\tMolecular weight:\t"<<oxidizer[i].mass<<" kg/mol"<<endl;
        i++;
    }
    oxidizerNum=i;
    //redirect
    cout<<"Done reading oxidizerData."<<endl;
    cin.rdbuf(oldcin);
    fin.close();
    return 0;
}

int readEngineDefinition(char *fileName)
{
    //trans filePath
    char filePath[256];
    strcpy(filePath,currentPath);
    strcat(filePath,fileName);
    cout<<"Reading engine definition from "<<filePath<<endl;
    //redirect
    ifstream fin(filePath);
    streambuf *oldcin;
    oldcin = cin.rdbuf(fin.rdbuf());
    string tempstr;
    int i=0;
    while(cin>>tempstr){
        if(tempstr=="chamberPressure") cin>>chamber.p;
        if(tempstr=="nozzleExitPressure") cin>>nozzleExit.p;
        if(tempstr=="freezingAtAt") cin>>frzFlow;
        if(tempstr=="optimumOFRatio") cin>>optimum;
        i++;
    }

    //redirect
    cout<<"Done reading engine definition."<<endl;
    cin.rdbuf(oldcin);
    fin.close();
    return 0;
}

int ele2Num(const string name)
{
    for(int i=0;i<eleNum;i++){
        if(name==ele[i].name) return ele[i].num;
    }
    return -1;
}
